<?php 
/* ------------------------------------------------
 * Home model class :
 *  + Create by tuanhnm
 * 
 * ------------------------------------------------
 * */


class Home_model extends CI_Model
{
	
	/*-----------------------
	 * Home model constructor
	 * ----------------------
	 * 
	 * */
	 function __construct(){
		 
		parent::__construct();
		  // Load Database util library
		 $this->load->database();
	}
	
	/*-----------------------
	 * set_hit_counter function
	 * ----------------------
	 * Object : check client ip , if don't exists, so save into users session
	 * 
	 * */
	 function set_hit_counter($client_data = array('client_ip' => NULL, 'computer_name' => NULL, 'time' => NULL))
	 {
		 $result = array(
			'error' => SUCCESS_MODE,
			'message' => ''
		 );
		 
		// Check if client ip empty to return 
		if (empty($client_data['client_ip']))
		{
			$result['error'] = ERROR_MODE;
			$result['message'] = IP_ADDRESS_EMPTY;
			
			return $result;
		}
		
		$l_client_data = $client_data;
		
		// Check client ip don't exits into database, save it
		$query = $this->db->get_where(USER_SESSION_TABLE_NAME, array(IP_ADDRESS_COL, $l_client_data['client_ip']), 1, 0);
		
		if ($query->num_rows() > 0)
		{
			$result['error'] = ERROR_MODE;
			$result['message'] = IP_ADDRESS_DUPLICATE;
			
			$query->free_result();
			
			return $result;
		}		
		
		// flush this previous query
		$query->free_result();
			
		// Save it
		$query = $this->db->insert(USER_SESSION_TABLE_NAME, $l_client_data);
		
		if ($query->num_rows() < 1)
		{
			$result['error'] = ERROR_MODE;
			$result['message'] = INSERT_CLIENT_DATA_FAILED;
			
			$query->free_result();
			
			return $result;
		}
		
		$result['message'] = INSERT_CLIENT_DATA_SUCCESS;
		
		return $result;
	 }
	
	function get_counter_by_id($counter_id=NULL, $language_type=NULL)
	{
		$result = array(
			'error' => SUCCESS_MODE,
			'item' => array(),
			'messages' => ''
		);
		
		if (empty($counter_id) || !is_numeric($counter_id) || empty( $language_type) || !is_numeric($language_type))
		{
			$result['error'] = ERROR_MODE;
			$result['messages'] = COUNTER_ID_EMPTY;
			return;
		}
		
		$l_counter_id = $counter_id;
		
		$l_language_type = $language_type;
		
		// Query image 's db
		$this->db->from(COUNTER_TABLE_NAME);
		
		$this->db->where(COUNTER_ID_COL, $l_counter_id);
		
		$query = $this->db->get();
		
		// Check if query hasn't any value 
		 if ($query->num_rows() < 1)
		 {
			 
			 $result['error'] = ERROR_MODE;
			 $result['messages'] = COUNTER_DATA_EMPTY;			 
			 
			 $query->free_result();
			 
			 return $result;
		 }
		
		$l_item = $query->row_array(0);		 
		 
		 $result['item'] = $l_item;		 		
		 
		 $query->free_result();
		 
		 return $result;
	}
	
	/* ---------------------------------------------------
	 * get_total_videos_by_name functions
	 * ---------------------------------------------------
	 * Object : 
	 * 		+ Response video 's product nums  .
	 * Parameters : 
	 * 		+ product_id : integer to store product 's id
	 * 		+ search_mode : 
	 * Return : 
	 * 		+ nums video' data
	 * Access : public
	 *  
	 * */ 
	 function get_total_items_by_name($item_name=NULL, $search_mode=NULL_MODE, $type=NULL)
	 {
		 // Check if video name empty and type empty so return 0;
		 if (empty($item_name) || empty($type))
		 {
			 return 0;
		 }
		 
		 $l_item_name = $item_name;
		 
		 $l_search_mode = $search_mode;
		 		 
		 $l_selected_table = $type;		 		 
		 
		 // Query image 's db
		 $this->db->from($l_selected_table);
		 
		 switch($l_search_mode)
		 {
			 case BEGIN_WITH_CHAR_MODE :
					$this->db->like(NAME_COL_VI, $l_item_name, 'before'); // sames as query : WHERE selected_col LIKE '% l_image_name'
					break;
			case END_WITH_CHAR_MODE :
					$this->db->like(NAME_COL_VI, $l_item_name, 'after'); // sames as query : WHERE selected_col LIKE 'l_image_name%'
					break;
			case EXACTLY_MODE :
					$this->db->like(NAME_COL_VI, $l_item_name); // sames as query : WHERE selected_col LIKE '% l_image_name%'
					break;
			case CONTAIN_CHAR_MODE :
					$this->db->like(NAME_COL_VI, $l_item_name, 'before');
					$this->db->or_like(NAME_COL_VI, $l_item_name, 'after');
					$this->db->or_like(NAME_COL_VI, $l_item_name);
					break;
			default : // get all item
					break;
		 } 
		 
		 $query = $this->db->get();
		 
		 $result = $query->num_rows();
		 
		 $query->free_result();
		 
		 return $result;
		 
		 		 
	 }
	
	/* ---------------------------------------------------
	 * get_videos_by_name functions
	 * ---------------------------------------------------
	 * Object : 
	 * 		+ Response images 's product item  .
	 * Parameters : 
	 * 		+ image_name : integer to store image 's name
	 * 		+ language_type : define language to return
	 * 		+ offset :
	 * 		+ limit :
	 * 		+ search_mode : 
	 * Return : 
	 * 		+ array data
	 * Access : public
	 *  
	 * */ 
	 function get_videos_by_name($video_name=NULL, $language_type=NULL, $offset=0, $limit=0, $search_mode=NULL_MODE)
	 {
		 $result = array(
			'items' => array(),
			'total' => 0,
			'error' => SUCCESS_MODE,
			'messages' => ''
		 );
		 
		 // Check if language type parameters not numberic is return empty data
		 if (empty($video_name))
		 {
			 $result['error'] = ERROR_MODE;
			 $result['messages'] = VIDEO_NAME_EMPTY;
			 return $result;
		 }
		 
		  // Check if language type parameters not numberic is return empty data
		 if (empty($language_type) || !is_numeric($language_type))
		 {
			 $result['error'] = ERROR_MODE;
			 $result['messages'] = LANG_NOT_NUMERIC;
			 return $result;
		 }
		 
		 $l_video_name = $video_name;
		 
		 $l_language_type = $language_type;
		 
		 $l_search_mode = $search_mode;
		 
		 $l_selected_col = ($l_language_type == EN) ? NAME_COL_EN : NAME_COL_VI;
		 
		  // Get total item per pages
		 $l_limit = $limit;
		 
		 // Get local offset 
		 $l_offset = $offset;			 
		 			 
		 // Query image 's db
		 $this->db->from(VIDEO_TABLE_NAME);
		 
		 switch($l_search_mode)
		 {
			 case BEGIN_WITH_CHAR_MODE :
					$this->db->like($l_selected_col, $l_video_name, 'before'); // sames as query : WHERE selected_col LIKE '% l_video_name'
					break;
			case END_WITH_CHAR_MODE :
					$this->db->like($l_selected_col, $l_video_name, 'after'); // sames as query : WHERE selected_col LIKE 'l_video_name%'
					break;
			case CONTAIN_CHAR_MODE :
					$this->db->like($l_selected_col, $l_video_name, 'before');
					$this->db->or_like($l_selected_col, $l_video_name, 'after');
					$this->db->or_like($l_selected_col, $l_video_name);
					break;
			case EXACTLY_MODE :
					$this->db->like($l_selected_col, $l_video_name); // sames as query : WHERE selected_col LIKE '% l_video_name%'
			default : // get all item
					break;
		 }
		 
		 $this->db->limit($l_limit, $l_offset);
		 
		 $query = $this->db->get();
					 					 
		 // Check if query hasn't any value 
		 if ($query->num_rows() < 1)
		 {
			 
			 $result['error'] = ERROR_MODE;
			 $result['messages'] = VIDEO_DATA_EMPTY;			 
			 $query->free_result();
			 
			 return $result;
		 }
		 
		 $l_items = $query->result_array();
		 
		 $l_totals = $this->get_total_items_by_name($l_video_name, $l_search_mode, VIDEO_TABLE_NAME);
		 
		 $result['items'] = $l_items;
		 
		 $result['total'] = $l_totals;
		 
		 $query->free_result();
		 
		 return $result;
	 }
	
	/* ---------------------------------------------------
	 * get_images_by_name functions
	 * ---------------------------------------------------
	 * Object : 
	 * 		+ Response images 's product item  .
	 * Parameters : 
	 * 		+ image_name : integer to store image 's name
	 * 		+ language_type : define language to return
	 * 		+ offset :
	 * 		+ limit :
	 * 		+ search_mode : 
	 * Return : 
	 * 		+ array data
	 * Access : public
	 *  
	 * */ 
	function get_images_by_name($image_name=NULL, $language_type=NULL, $offset=0, $limit=0, $search_mode=NULL_MODE)
	 {
		 $result = array(
			'items' => array(),
			'total' => 0,
			'error' => SUCCESS_MODE,
			'messages' => ''
		 );
		 
		 // Check if language type parameters not numberic is return empty data
		 if (empty($image_name))
		 {
			 $result['error'] = ERROR_MODE;
			 $result['messages'] = IMAGE_NAME_EMPTY;
			 return $result;
		 }
		 
		  // Check if language type parameters not numberic is return empty data
		 if (empty($language_type) || !is_numeric($language_type))
		 {
			 $result['error'] = ERROR_MODE;
			 $result['messages'] = LANG_NOT_NUMERIC;
			 return $result;
		 }
		 
		 $l_image_name = $image_name;
		 
		 $l_language_type = $language_type;
		 
		 $l_search_mode = $search_mode;
		 
		 $l_selected_col = ($l_language_type == EN) ? NAME_COL_EN : NAME_COL_VI;
		 
		  // Get total item per pages
		 $l_limit = $limit;
		 
		 // Get local offset 
		 $l_offset = $offset;			 
		 			 
		 // Query image 's db
		 $this->db->from(IMAGE_TABLE_NAME);
		 
		 switch($l_search_mode)
		 {
			 case BEGIN_WITH_CHAR_MODE :
					$this->db->like($l_selected_col, $l_image_name, 'before'); // sames as query : WHERE selected_col LIKE '% l_image_name'
					break;
			case END_WITH_CHAR_MODE :
					$this->db->like($l_selected_col, $l_image_name, 'after'); // sames as query : WHERE selected_col LIKE 'l_image_name%'					
					break;
			case EXACTLY_MODE :
					$this->db->like($l_selected_col, $l_image_name); // sames as query : WHERE selected_col LIKE '% l_image_name%'
					break;
			case CONTAIN_CHAR_MODE :
					$this->db->like($l_selected_col, $l_image_name, 'before');
					$this->db->or_like($l_selected_col, $l_image_name, 'after');
					$this->db->or_like($l_selected_col, $l_image_name);
					break;
			default : // get all item
					break;
		 }

		 $this->db->limit($l_limit, $l_offset);
		 
		 $query = $this->db->get();
					 					 
		 // Check if query hasn't any value 
		 if ($query->num_rows() < 1)
		 {
			 
			 $result['error'] = ERROR_MODE;
			 $result['messages'] = IMAGE_DATA_EMPTY;			 
			 $query->free_result();
			 
			 return $result;
		 }
		 
		 $l_items = $query->result_array();
		 
		 $l_totals = $this->get_total_items_by_name($l_image_name, $l_search_mode, IMAGE_TABLE_NAME);
		 
		 $result['items'] = $l_items;
		 	
		 $result['total'] = $l_totals;
		 
		 $query->free_result();
		 
		 return $result;
	 }

	/* ---------------------------------------------------
	 * Get videos by proid videoid functions
	 * ---------------------------------------------------
	 * Object : 
	 * 		+ Response video 's product item  .
	 * Parameters : 
	 * 		+ product_id : integer to store product 's id
	 * 		+ id : integer to store video's id
	 * Return : 
	 * 		+ objects images' data
	 * Access : public
	 *  
	 * */
	 function get_video_by_proid_id($product_id=NULL, $video_id=NULL, $language_type=NULL)
	 {
		  // Check if $product id parameters not numberic is return empty data
		 if (empty($product_id) || empty($video_id) || !is_numeric($product_id) || !is_numeric($video_id) || !is_numeric($language_type))
		 {
			 return NULL;
		 }		
		
		 $l_product_id = $product_id;
		 
		 $l_video_id = $video_id;
		 
		 $l_language_type = empty($language_type) ? VN : EN;
		 			 
		 // Query image 's db
		 $this->db->from(VIDEO_TABLE_NAME);
		 
		 $this->db->where('id', $l_video_id);
		 
		 $this->db->where('product_id', $l_product_id);		 	
		 
		 $query = $this->db->get();
					 					 
		 // Check if query hasn't any value 
		 if ($query->num_rows() < 1)
		 {
			 
			 $query->free_result();
			 
			 return NULL;
		 }
		 
		 $first_row = $query->row(0);
		 
		 $l_video_name = ($language_type == EN) ?  $first_row->name_en : $first_row->name ; // Default get vietnamese 's name
		 $l_video_url = $first_row->url;
		 $l_video_image = $first_row->image;
		 
		 $result = array(
			'video_name' => $l_video_name,
			'video_url' => $l_video_url,// wrapper returned name in result array
			'video_image' => $l_video_image
		 );
		 
		 $query->free_result();
		 
		 return $result;
	 }
	
	/* ---------------------------------------------------
	 * Get items array functions
	 * ---------------------------------------------------
	 * Object : 
	 * 		+ Response items array by offset, limit   .
	 * Parameters : 
	 * 		+ $source : array which need to get items
	 * 		+ $offset : start point to get items
	 * 		+ $limit : items per pages (end point = offset + limit)
	 * Return : 
	 * 		+ array 
	 * Access : public
	 *  
	 * */ 
	 function get_items_array($source=NULL, $offset=0, $limit=0)
	 {
		 $result = array(
			'items' => array(),
			'error' => 0,
			'message' => ''
		 );
		 // Check if sources empty to return NULL
		 if (empty($source))
		 {
			 $result['error'] = 1;
			 $result['message'] = ITEMS_DATA_EMPTY;
			 return $result;
		 }
		 
		 $l_source = $source;
		 $l_offset = $offset;
		 $l_limit = $limit;
		 
		 $l_sub_source = array_slice($l_source, $l_offset, $l_limit);
		 
		 $result['items'] = $l_sub_source;
		 
		 return $result;
	 }
	 
	 /* ---------------------------------------------------
	 * Get images by proid imgid functions
	 * ---------------------------------------------------
	 * Object : 
	 * 		+ Response images 's product item  .
	 * Parameters : 
	 * 		+ product_id : integer to store product 's id
	 * Return : 
	 * 		+ objects images' data
	 * Access : public
	 *  
	 * */
	 function get_image_by_proid_id($product_id=NULL, $image_id=NULL, $language_type=NULL)
	 {
		 // Check if $product id parameters not numberic is return empty data
		 if (empty($product_id) || empty($image_id) || !is_numeric($product_id) || !is_numeric($image_id) || !is_numeric($language_type))
		 {
			 return NULL;
		 }		
		
		 $l_product_id = $product_id;
		 
		 $l_image_id = $image_id;
		 
		 $l_language_type = empty($language_type) ? VN : EN;
		 			 
		 // Query image 's db
		 $this->db->from(IMAGE_TABLE_NAME);
		 
		 $this->db->where('id', $l_image_id);
		 
		 $this->db->where('product_id', $l_product_id);		 	
		 
		 $query = $this->db->get();
					 					 
		 // Check if query hasn't any value 
		 if ($query->num_rows() < 1)
		 {
			 
			 $query->free_result();
			 
			 return NULL;
		 }
		 
		 $first_row = $query->row(0);
		 
		 $l_image_name = ($language_type == EN) ?  $first_row->name_en : $first_row->name ; // Default get vietnamese 's name
		 $l_image_url = $first_row->url;
		 
		 $result = array(
			'image_name' => $l_image_name,
			'image_url' => $l_image_url // wrapper returned name in result array
		 );
		 
		 $query->free_result();
		 
		 return $result;
	 } 
	
	/* ---------------------------------------------------
	 * Get content production functions
	 * ---------------------------------------------------
	 * Object : 
	 * 		+ Response content information 's product item   .
	 * Parameters : 
	 * 		+ product_id : integer to store product 's id
	 * 		+ language_type : integer to response corresponding data
	 * Return : 
	 * 		+ array 
	 * Access : public
	 *  
	 * */ 
	 function get_product_content($product_id=NULL, $language_type=NULL)
	 {
		  // Check if product_id is null to return
		if (empty($product_id) || !is_numeric($product_id) || !is_numeric($language_type))
		{
			return NULL;					
		}	
		 
		$l_product_id = $product_id;
		
		$l_language_type = empty($language_type) ? VN : $language_type;  // default language vietnamese
		 
		// Query image 's db
		 $this->db->from(PRODUCT_TABLE_NAME);
		 
		 $this->db->where('id', $product_id);		 
		 
		 $query = $this->db->get();
		 
		 // Check if hasn't returned data
		 if ($query->num_rows() < 1)
		 {
			 $query->free_result();
			 
			 return NULL;
		 }
		 
		 // Prepare data to return
		 
		 $first_row = $query->row(0); // Default get first row
		 
		 $l_product_content = ($language_type == EN) ?  $first_row->content_en : $first_row->content ; // Default get vietnamese 's name
		 $l_product_name = ($language_type == EN) ?  $first_row->name_en : $first_row->name;
		 
		 $result = array(
			'name' => $l_product_name,
			'product_content' => $l_product_content // wrapper returned name in result array
		 );
		 
		 $query->free_result(); // free memory
		 
		 return $result;
	 }
	
	/* ---------------------------------------------------
	 * Get name production functions
	 * ---------------------------------------------------
	 * Object : 
	 * 		+ Response name information 's product item   .
	 * Parameters : 
	 * 		+ product_id : integer to store product 's id
	 * 		+ language_type : integer to response corresponding data
	 * Return : 
	 * 		+ array 
	 * Access : public
	 *  
	 * */ 
	 function get_product_name($product_id=NULL, $language_type=NULL)
	 {
		 // Check if product_id is null to return
		if (empty($product_id) || !is_numeric($product_id) || !is_numeric($language_type))
		{
			return NULL;					
		}	
		 
		$l_product_id = $product_id;
		
		$l_language_type = empty($language_type) ? VN : $language_type;  // default language vietnamese
		 
		// Query image 's db
		 $this->db->from(PRODUCT_TABLE_NAME);
		 
		 $this->db->where('id', $product_id);		 
		 
		 $query = $this->db->get();
		 
		 // Check if hasn't returned data
		 if ($query->num_rows() < 1)
		 {
			 $query->free_result();
			 
			 return NULL;
		 }
		 
		 // Prepare data to return
		 
		 $first_row = $query->row(0); // Default get first row
		 
		 $l_product_name = ($language_type == EN) ?  $first_row->name_en : $first_row->name ; // Default get vietnamese 's name
		 
		 $result = array(
			'product_name' => $l_product_name // wrapper returned name in result array
		 );
		 
		 $query->free_result(); // free memory
		 
		 return $result;
	 }
	
	/* ---------------------------------------------------
		 * get_total_videos_by_proid functions
		 * ---------------------------------------------------
		 * Object : 
		 * 		+ Response video 's product nums  .
		 * Parameters : 
		 * 		+ product_id : integer to store product 's id
		 * Return : 
		 * 		+ nums video' data
		 * Access : public
		 *  
		 * */ 
	function get_total_items_by_proid($product_id=NULL, $type=NULL)
	{
		// Check if product_id is null to return
		if (empty($product_id) || empty($type))
		{
			return 0;
		}	
		
		$selected_table = ($type == VIDEO_TABLES) ? VIDEO_TABLE_NAME : IMAGE_TABLE_NAME;
		
		 // Query image 's db
		 $this->db->from($selected_table);
		 
		 $this->db->where('product_id', $product_id);		 
		 
		 $query = $this->db->get();
		 
		 $result = $query->num_rows();
		 
		 $query->free_result();
		 
		 return $result;
	}
	
	/* ---------------------------------------------------
		 * get_videos_by_proid functions
		 * ---------------------------------------------------
		 * Object : 
		 * 		+ Response video 's product item  .
		 * Parameters : 
		 * 		+ product_id : integer to store product 's id
		 * Return : 
		 * 		+ objects video' data
		 * Access : public
		 *  
		 * */  
		 function get_videos_by_proid($product_id=NULL, $offset=NULL)
		 {
			 // Check if $product id parameters not numberic is return empty data
			 if ( !is_numeric($product_id) || !is_numeric($offset))
			 {
				 return NULL;
			 }
			 
			 // Get total item per pages
			 $l_videos_per_pages = $this->config->item('videos_per_page');
			 
			 // Get local offset 
			 $l_offset = $offset;		
			 			 
			 // Query image 's db
			 $this->db->from(VIDEO_TABLE_NAME);
			 
			 $this->db->where('product_id', $product_id);
			 
			 $this->db->limit($l_videos_per_pages, $l_offset);
			 
			 $query = $this->db->get();			 
						 					 
			 // Check if query hasn't any value 
			 if ($query->num_rows() < 1)
			 {
				 
				 $query->free_result();
				 
				 return null;
			 }
			 
			 $result = $query->result();			 
			 
			 $query->free_result();
			 
			 return $result;			 
		 }
	
	/* ---------------------------------------------------
		 * get_images_by_proid functions
		 * ---------------------------------------------------
		 * Object : 
		 * 		+ Response images 's product item  .
		 * Parameters : 
		 * 		+ product_id : integer to store product 's id
		 * Return : 
		 * 		+ objects images' data
		 * Access : public
		 *  
		 * */  
		 function get_images_by_proid($product_id=NULL, $offset=NULL)
		 {
			 // Check if $product id parameters not numberic is return empty data
			 if (!is_numeric($product_id) || !is_numeric($offset))
			 {
				 return NULL;
			 }
			 
			  // Get total item per pages
			 $l_images_per_pages = $this->config->item('images_per_page');
			 
			 // Get local offset 
			 $l_offset = $offset;			 
			 			 
			 // Query image 's db
			 $this->db->from(IMAGE_TABLE_NAME);
			 
			 $this->db->where('product_id', $product_id);
			 
			 $this->db->limit($l_images_per_pages, $l_offset);
			 
			 $query = $this->db->get();
						 					 
			 // Check if query hasn't any value 
			 if ($query->num_rows() < 1)
			 {
				 
				 $query->free_result();
				 
				 return null;
			 }
			 
			 $result = $query->result();
			 
			 $query->free_result();
			 
			 return $result;			 
		 }

	  /* ---------------------------------------------------
		 * get_module_content functions
		 * ---------------------------------------------------
		 * Object : 
		 * 		+ Response content 's module page .
		 * Parameters : 
		 * 		+ $module_id : define module to get content.
		 * 		+ $language_type : define language to returned content
		 * Return : 
		 * 		+ array data from db
		 * Access : public
		 *  
		 * */  
		 function get_module_content($module_id=NULL, $language_type=NULL)
		 {
			 // Check if $type isn't nums to returned
			 if (empty($module_id) || !is_numeric($module_id) || !is_numeric($language_type))
			 {
				 return NULL;
			 }		 
			 
			 $l_module_id = $module_id;
			 
			 $l_language_type = empty($language_type) ? VN : $language_type ; // Default language
			 
			 // Query database to get content's index
			 
			 $this->db->from(MODULE_TABLE_NAME);
			 
			 $this->db->where('id', $l_module_id);
			 
			 $query = $this->db->get();
						 		
			 
			 // Check if query hasn't any value 
			 if ($query->num_rows() < 1)
			 {
				 $query->free_result();
				 return NULL;
			 }
			 
			 // Get first row from rows 
			 $first_row = $query->row(0);
			 
			 $l_module_content = ($language_type == EN) ?  $first_row->content_en : $first_row->content ; // Default get vietnamese 's name
			 
			 $l_module_name = ($language_type == EN) ?  $first_row->name_en : $first_row->name ; // Default get vietnamese 's name
		 
			 $result = array(
				'name' => $l_module_name,
				'module_content' => $l_module_content // wrapper returned name in result array
			 );
			 			 			
			 $query->free_result();			 
			 
			 return $result;
			 			 
		 }
	
}
?>
